import socket
import os

addr = '192.168.1.15'
port = 913
s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IP)

s.bind((addr, port))

# s.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL,1)

data = s.recv(1024)
print(data)
res = str(bin(data[0]))[2:].zfill(8)
IP_version = int(res[0:4],2)
IP_head_len = int(res[4:],2)*4

server_kind = data[1]

total_len = int(str(bin(data[2]))[2:].zfill(8)+str(bin(data[3]))[2:].zfill(8),2)

identification = int(str(bin(data[4]))[2:].zfill(8)+str(bin(data[5]))[2:].zfill(8),2)

t = str(bin(data[6]))[2:].zfill(8)+str(bin(data[7]))[2:].zfill(8)
flag = int(t[0:3],2)
MF = t[2]
DF = t[1]
Fragment_offset = int(t[3:],2)

TTL = data[8]
protocol = data[9]

checkSum = int(str(bin(data[10]))[2:].zfill(8)+str(bin(data[11]))[2:].zfill(8),2)

src_ip = str(int(str(bin(data[12]))[2:].zfill(8),2))+"."+str(int(str(bin(data[13]))[2:].zfill(8),2))+\
"."+str(int(str(bin(data[14]))[2:].zfill(8),2))+"."+str(int(str(bin(data[15]))[2:].zfill(8),2))

dst_ip = str(int(str(bin(data[16]))[2:].zfill(8),2))+"."+str(int(str(bin(data[17]))[2:].zfill(8),2))+\
"."+str(int(str(bin(data[18]))[2:].zfill(8),2))+"."+str(int(str(bin(data[19]))[2:].zfill(8),2))

src_port = int(str(bin(data[20]))[2:].zfill(8)+str(bin(data[21]))[2:].zfill(8),2)
dst_port = int(str(bin(data[22]))[2:].zfill(8)+str(bin(data[23]))[2:].zfill(8),2)

seq = int(str(bin(data[24]))[2:].zfill(8)+str(bin(data[25]))[2:].zfill(8)+str(bin(data[26]))[2:].zfill(8)+str(bin(data[27]))[2:].zfill(8),2)
ack = int(str(bin(data[28]))[2:].zfill(8)+str(bin(data[29]))[2:].zfill(8)+str(bin(data[30]))[2:].zfill(8)+str(bin(data[31]))[2:].zfill(8),2)

t = str(bin(data[32]))[2:].zfill(8)+str(bin(data[33]))[2:].zfill(8)
TCP_head_len = int(t[0:4],2)*4
keep = int(t[4:10],2)
URG = t[10]
ACK = t[11]
PSH = t[12]
RST = t[13]
SYN = t[14]
FIN = t[15]
windowSize = int(str(bin(data[34]))[2:].zfill(8)+str(bin(data[35]))[2:].zfill(8),2)
checkSum = int(str(bin(data[36]))[2:].zfill(8)+str(bin(data[37]))[2:].zfill(8),2)
UrgPtr = int(str(bin(data[38]))[2:].zfill(8)+str(bin(data[39]))[2:].zfill(8),2)

other_len = TCP_head_len-20
other = ''
for i in range(other_len):
    other += str(bin(data[40+i]))
TCP_data = data[(IP_head_len+TCP_head_len):].decode('utf-8')

os.remove("./logs.txt")
myfile = open("./logs.txt",'a',encoding='utf-8')
print("/////////// IP头部 /////////////////////",file=myfile)
print("IP_version: ",IP_version,file=myfile)
print("IP_head_len: ",IP_head_len,file=myfile)
print("server_kind: ",server_kind,file=myfile)
print("total_len: ",total_len,file=myfile)
print("identification: ",identification,file=myfile)
print("flag: %d (其中MF=%s,DF=%s)"%(flag,MF,DF),file=myfile)
print("Fragment_offset: ",Fragment_offset,file=myfile)
print("TTL: ",TTL,file=myfile)
print("protocol: ",protocol,file=myfile)
print("checkSum: ",checkSum,file=myfile)
print("src_ip: ",src_ip,file=myfile)
print("dst_ip: ",dst_ip,file=myfile)
print("ip_data: ",file=myfile)
print("/////////// IP数据部分 /////////////////////",file=myfile)
print("src_port: ",src_port,file=myfile)
print("dst_port: ",dst_port,file=myfile)
print("seq: ",seq,file=myfile)
print("ack: ",ack,file=myfile)
print("TCP_head_len: ",TCP_head_len,file=myfile)
print("keep: ",keep,file=myfile)
print("URG=%s,ACK=%s,PSH=%s,RST=%s,SYN=%s,FIN=%s"%(URG,ACK,PSH,RST,SYN,FIN),file=myfile)
print("windowSize: ",windowSize,file=myfile)
print("checkSum: ",checkSum,file=myfile)
print("UrgPtr: ",UrgPtr,file=myfile)
print("other: ",other,file=myfile)
print("data: ",TCP_data,file=myfile)

myfile.close()

myfile = open("./logs.txt","r",encoding="utf-8")
res = myfile.read()
print(res)

